package org.alvin.i18n.newui;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class MergeJSon {

    public static void main(String[] args) throws IOException {
        JSONObject merge1 = JSONObject.parseObject(new String(Files.readAllBytes(Paths.get("D:\\merge\\i18n_v3_old_hk.js"))));
        JSONObject merge2 = JSONObject.parseObject(new String(Files.readAllBytes(Paths.get("D:\\merge\\i18n_v3_hk.js"))));
        List<String> list = Lists.newLinkedList();
        for (String key : merge1.keySet()) {
            System.out.println("key:" + key);
            if (list.contains(key)) {
                System.out.println("重复的key:" + key);
            }
            list.add(key);
            if (merge2.containsKey(key)) {
                System.out.println("合并的key:" + key);
                JSONObject jsonObject = merge1.getJSONObject(key);
                jsonObject.putAll(merge2.getJSONObject(key));
                merge1.put(key, jsonObject);
                merge2.remove(key);
            }
        }
        for (String key : merge2.keySet()) {
            System.out.println("追加的key:" + key);
            merge1.put(key, merge2.get(key));
        }
        Path path = Paths.get("D:\\merge\\zi18n_v3_new_hk.js");
        String outString = JSON.toJSONString(merge1, SerializerFeature.PrettyFormat, SerializerFeature.MapSortField);
        Files.write(path, outString.getBytes(StandardCharsets.UTF_8));
    }
}
